 # -*- coding: utf-8 -*-
"""
Created on Wed Aug 21 17:11:08 2019

@author: admin
"""

from elasticsearch import Elasticsearch
import pymysql
import time
import json
import os
 
relation_mapping = [['包含','CONTAINS_ANY'],['包含所有','CONTAINS_ALL'],
                    ['不包含','NOT_CONTAINS_ANY'],['不包含所有','NOT_CONTAINS_ALL'],
                    ['等于','EQUAL'],['不等于','NOT_EQUAL'],
                    ['大于','GRATER'],['大于等于','EQUAL_GRATER'],
                    ['小于','LESS'],['小于等于','EQUAL_LESS'],
                    ['包含','LIST_ANY'],['不包含','NOT_LIST_ANY']
                    ]
entity_mapping = [['tags','项目'],['customer','客户'],['supplier','厂家']]

#关键词映射
def mapping(keyword, list_mapping):
    for l in list_mapping:
        if keyword == l[1]:
            break
    return l[0]

def rule_word_get():
    db = pymysql.connect('47.112.138.184','dict','tisson1!','crawler',3308)
    cursor = db.cursor()
    
    rule_label_all = []
    word_text_all = []
    
    #获取标签的优先级
    cursor.execute('SELECT TAG_VALUE_ID FROM t_tag_sync_seq ORDER BY TAG_ORDER')
    t_tag_sync_seq = cursor.fetchall()
    t_tag_sync_seq = [t[0] for t in t_tag_sync_seq]
    
    for t_seq in t_tag_sync_seq:
        cursor.execute('SELECT * FROM t_tag_value WHERE ID='+str(t_seq)+'')        
        t_tag_value = cursor.fetchone()
        #获取未过期标签
        if t_tag_value[5] == 0:
            continue        
        label = t_tag_value[2]
        #获取实体
        cursor.execute('SELECT * FROM t_tag WHERE ID='+str(t_tag_value[1])+'')
        entity = cursor.fetchone()[1][:-2]
        entity = mapping(entity, entity_mapping)
        #每个标签对应多条规则
        cursor.execute('SELECT * FROM t_tag_rule WHERE TAG_VALUE_ID='+str(t_tag_value[0])+'')
        list_num_rule = [cur[2] for cur in cursor.fetchall()]
        #每条规则中的子规则相与
        for num_rule in list_num_rule:
            cursor.execute('SELECT * FROM t_meta_rule WHERE PARENT_ID='+str(num_rule)+'')
            list_rule = cursor.fetchall()
            list_rule_joint = []
            list_rule_joint_text = []
            for rule in list_rule:
                name = rule[1]
                relation = mapping(rule[2], relation_mapping)
                if rule[2] == 'LIST_ANY' or rule[2] == 'NOT_LIST_ANY':
                    cursor.execute('SELECT LIST_VALUE FROM t_static_list WHERE LIST_NAME="'+rule[3]+'"')
                    values = [c[0] for c in cursor.fetchall()]
                    #去除空字符
                    values = [value for value in values if value]
                else:
                    values = rule[3].split(',')
                    #去除空字符
                    values = [value for value in values if value]
                #如果有全文文本的规则，将其放在最后
                if name == 'notice.text':
                    dict_rule = {'notice.notice_id'+'|'+relation:values}
                    list_rule_joint_text.append(dict_rule)
                    word_text_all.append(values)
                else:
                    dict_rule = {name+'|'+relation:values}
                    list_rule_joint.append(dict_rule)
            list_rule_joint.extend(list_rule_joint_text)
            #得到一条完整的规则
            rule_label = {
                    label:
                        [
                            list_rule_joint,
                            entity
                        ]
                    }
            rule_label_all.append(rule_label)
            
    cursor.close()
    db.close()
    return rule_label_all, word_text_all

def rule_word_transform(is_save=False):
    start = time.time()
    es_access = 'http://47.112.138.184:9202'
    es_index = 'htmltext'
    doc_type = 'doc'    
    _es = Elasticsearch([es_access], http_auth=('dict', 'tisson1!es'), timeout=60, max_retries=3, retry_on_timeout=True)
    
    rules, words = rule_word_get()
    id_text_match = {}
    
    for word in words:
        body = {'_source':'id','query':{'bool':{'should':[]}}}
        for w in word:
            body_temp = {'match_phrase': {'text': w}}
            body['query']['bool']['should'].append(body_temp)
        search_result = _es.search(index=es_index, doc_type=doc_type, body=body, size=10000)
        hit = search_result['hits']['hits']
        temp = []
        for h in hit:
            _id = h['_source']['id']
            if _id:
                temp.append(_id)
        id_text_match['|'.join(word)] = temp
        
    if is_save:
        filedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'market_log')
        filepath = os.path.join(filedir, 'label_rule_mysql.json')        
        with open(filepath, 'w') as f:
            json.dump(rules, f)
        filepath = os.path.join(filedir, 'label_word_html.json')
        with open(filepath, 'w') as f:
            json.dump(id_text_match, f)
    end = time.time()
    print('Get Rules And Words Sucessfully')
    print('Use Time: %s' % str(end-start))
    return rules, id_text_match

if __name__ == '__main__':
    rules, id_text_match = rule_word_transform()
    #rule_label_all, word_text_all = rule_word_get()
    #print(id_text_match)
    #rule_word_get()

            


      
